package com.pachong.android.framework.share;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.cncn.youmengsharelib.BuildConfig;
import com.cncn.youmengsharelib.ShareData;
import com.cncn.youmengsharelib.ShareHelper;
import com.cncn.youmengsharelib.ShareListener;
import com.cncn.youmengsharelib.SharePlatform;
import com.pachong.android.framework.R;
import com.pachong.android.frameworkbase.dialog.base.BottomDialogFragment;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;

/**
 * 第三方分享平台选择界面
 *
 * @Author: PengWeiwei
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2017-01-06
 * @Version: 1.0.0
 * <p>
 * 使用方法：
 * 覆盖getShareLayoutId，返回一个自定义的布局，参考fragment_base_share_platforms，布局中的UI控件id设置成已经定义好的id。  *
 */
public class BaseShareFragment extends BottomDialogFragment implements View.OnClickListener {

    public static final String EXTRA_SHAER_INFO = "extra_share_info";

    private ShareData mShareData;
    //一个对Context的软引用
    private SoftReference<Context> mAppRef;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @SuppressLint("ValidFragment")
    public BaseShareFragment(String title, String content, Bitmap imgBmp, String targetUrl) {
        mShareData = new ShareData(title, content, imgBmp, targetUrl);
    }

    @SuppressLint("ValidFragment")
    public BaseShareFragment(String title, String content, String imageUrl, String targetUrl) {
        mShareData = new ShareData(title, content, imageUrl, targetUrl);
    }

    @SuppressLint("ValidFragment")
    public BaseShareFragment(ShareData shareData) {
        mShareData = shareData;
    }


    public BaseShareFragment() {
        super();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View rootView = inflater.inflate(getShareLayoutId(), container, false);
        mAppRef = new SoftReference<>(getActivity().getApplicationContext());
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    protected void initView(View rootView) {
        setOnClickListener(rootView, R.id.common_share_qq);
        setOnClickListener(rootView, R.id.common_share_qzone);
        setOnClickListener(rootView, R.id.common_share_wechat);
        setOnClickListener(rootView, R.id.common_share_wxcircle);
        setOnClickListener(rootView, R.id.common_share_sms);
        setOnClickListener(rootView, R.id.common_share_sina_weibo);
    }

    private void setOnClickListener(View rootView, @IdRes int viewId) {
        View view = rootView.findViewById(viewId);
        if (view != null) {
            view.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.common_share_qq) {
            onShareQQClicked();
        } else if (i == R.id.common_share_qzone) {
            onShareQZoneClicked();
        } else if (i == R.id.common_share_wechat) {
            onShareWechatClicked();
        } else if (i == R.id.common_share_wxcircle) {
            onShareWXCircleClicked();
        } else if (i == R.id.common_share_sms) {
            onShareSmsClicked();
        } else if (i == R.id.common_share_sina_weibo) {
            onShareSinaWeiboClicked();
        }

    }

    protected void onShareSinaWeiboClicked() {
        log(" onShareSinaWeiboClicked  ");
        ShareHelper.shareSinaWeibo(getActivity(), mShareData, mShareListener);
    }

    protected void onShareQQClicked() {
        log(" onShareQQClicked  ");
        ShareHelper.shareQQ(getActivity(), mShareData, mShareListener);
    }


    protected void onShareQZoneClicked() {
        log(" onShareQZoneClicked  ");
        ShareHelper.shareQzone(getActivity(), mShareData, mShareListener);
    }

    protected void onShareWechatClicked() {
        log(" onShareWechatClicked  ");
        ShareHelper.shareWechat(getActivity(), mShareData, mShareListener);
    }

    protected void onShareWXCircleClicked() {
        log(" onShareWXCircleClicked  ");
        ShareHelper.shareWechatMoments(getActivity(), mShareData, mShareListener);
    }

    protected void onShareSmsClicked() {
        log(" onShareSmsClicked  ");
        ShareHelper.shareSMS(getActivity(), mShareData, mShareListener);
    }


    public @LayoutRes int getShareLayoutId() {
        return R.layout.fragment_base_share_platforms;
    }

    /**
     * 外部设置监听器
     *
     * @param shareListener
     */
    public void setShareListener(ShareListener shareListener) {
        mShareListener = shareListener;
    }

    private ShareListener mShareListener = new ShareListener() {

        @Override
        public void onShareSuccess(Activity shareActivity, SharePlatform platform) {
            Context context = mAppRef.get();
            Toast.makeText(context, "分享成功", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onShareFailed(Activity shareActivity, SharePlatform platform) {
            Context context = mAppRef.get();
            Toast.makeText(context, "分享失败", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onShareCancel(Activity shareActivity, SharePlatform platform) {
            Context context = mAppRef.get();
            Toast.makeText(context, "取消分享", Toast.LENGTH_SHORT).show();
        }
    };


    public static List<SharePlatform> getDefaultPlatformInfoes() {
        SharePlatform info1 = SharePlatform.QQ;
        SharePlatform info2 = SharePlatform.QZONE;
        SharePlatform info3 = SharePlatform.WECHAT;
        SharePlatform info4 = SharePlatform.WECHATMOMENTS;
        SharePlatform info5 = SharePlatform.SINAWEIBO;
        SharePlatform info6 = SharePlatform.SMS;
        // 设置显示的图片
        info1.setDrawableId(R.drawable.umeng_socialize_qq_on);


        List<SharePlatform> list = new ArrayList<>();
        list.add(info1);
        list.add(info2);
        list.add(info3);
        list.add(info4);
        list.add(info5);
        list.add(info6);

        return list;

    }



    private static final String TAG = "Share";

    public static void log(String text) {
        if (BuildConfig.DEBUG) {
            Log.d(TAG, text);
        }
    }
}
